﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using NJT.Services;

namespace FM.播音.Services
{
    public class 动画类
    {
        private int _动画毫秒 = 800;
        private double h = 100;
        private double w = 150;
        private FrameworkElement 动画1;
        private Storyboard 动画组;
        private DoubleAnimation 左动画;
        private DoubleAnimation 顶动画;


        public 动画类(FrameworkElement 画布cs, FrameworkElement 动画cs)
        {
            初始化(画布cs, 动画cs);
        }
        public int 动画毫秒
        {
            get { return _动画毫秒; }
            set { _动画毫秒 = value; }
        }

        private void 初始化(FrameworkElement 画布1, FrameworkElement 动画cs)
        {
            动画1 = 动画cs;
            NameScope.SetNameScope(画布1, new NameScope());
            画布1.RegisterName(动画cs.Name, 动画cs);

            左动画 = new DoubleAnimation();
            左动画.From = 500;
            左动画.To = w;
            左动画.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 动画毫秒));
            Storyboard.SetTargetName(左动画, 动画cs.Name);
            Storyboard.SetTargetProperty(左动画, new PropertyPath(Canvas.LeftProperty));

            顶动画 = new DoubleAnimation();
            顶动画.From = 500;
            顶动画.To = h;
            顶动画.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 动画毫秒));
            Storyboard.SetTargetName(顶动画, 动画cs.Name);
            Storyboard.SetTargetProperty(顶动画, new PropertyPath(Canvas.TopProperty));

            动画组 = new Storyboard();
            动画组.Children.Add(顶动画);
            动画组.Children.Add(左动画);
            动画组.RepeatBehavior = new RepeatBehavior(1);
            动画组.Completed += (o, e) => 动画cs.Visibility = Visibility.Hidden;
        }

        public void 启动(Point 源point, Point 目标point)
        {
            double 角度2 = 角度计算.时钟角度(源point, 目标point);
            动画1.RenderTransform = new RotateTransform(角度2);
            动画1.Visibility = Visibility.Visible;

            左动画.From = 源point.X;
            左动画.To = 目标point.X;
            顶动画.From = 源point.Y;
            顶动画.To = 目标point.Y;

            动画组.Begin(动画1);
        }
    }
}
